package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.UserMapper;
import com.qf.pojo.UserPojo;
import com.qf.service.UserService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import com.qf.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPojo> implements UserService {
    @Override
    public ResultMsg login(String userName, String userPass, HttpServletRequest request) {
        QueryWrapper<UserPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        wrapper.eq("user_pass", userPass);
        UserPojo one = this.getOne(wrapper);
        if (one != null) {
            try {
                one.setToken(TokenUtils.createToken(one.getUserId() + "", one.getUserPass()));
                HttpSession session = request.getSession();
                session.setAttribute(Constant.USER_SESSION, one);
                return ResultMsg.SUCCESS(one);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }

        }
        return ResultMsg.FAILED(Constant.LZY + 2, "用户名或密码错误");
    }

    @Override
    public ResultMsg updateUserPass(String oldPass, String newPass, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserPojo user = (UserPojo) session.getAttribute(Constant.USER_SESSION);
//        user.setUserId(1);
//        UserPojo user= this.getOne(new QueryWrapper<UserPojo>().eq("user_id", 1));
        if (!user.getUserPass().equals(oldPass))
            return ResultMsg.FAILED(Constant.LZY + 2, "旧密码输入错误");
        user.setUserPass(newPass);
        boolean b = this.updateById(user);
        if (b)
            return ResultMsg.SUCCESS(user);
        return ResultMsg.FAILED(Constant.LZY + 1, "未知错误");
    }

    @Override
    public ResultMsg queryOneUser(Integer userId) {
        QueryWrapper<UserPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        UserPojo one = this.getOne(wrapper);
        if (one != null)
            return ResultMsg.SUCCESS(one);
        return ResultMsg.FAILED(Constant.LZY + 1, "未知错误");
    }

    @Override
    public ResultMsg updateUser(UserPojo userPojo) {
        if (userPojo.getUserName() == null)
            return ResultMsg.FAILED(Constant.LZY + 1, "用户名输入有误");
        boolean b = this.updateById(userPojo);
        if (b)
            return ResultMsg.SUCCESS();
        return ResultMsg.FAILED(Constant.LZY + 1, "未知错误");
    }

    @Override
    public ResultMsg logout(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            session.setAttribute(Constant.USER_SESSION, null);
            return ResultMsg.SUCCESS();
        } catch (RuntimeException e) {
            return ResultMsg.FAILED(Constant.LZY + 2, "未知错误");
        }
    }

    @Override
    public ResultMsg findAllDoctor() {
        QueryWrapper<UserPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_job", "主治医生");
        List<UserPojo> list = this.list(wrapper);
        return ResultMsg.SUCCESS(list);
    }
}
